package com.jing.myservice.service;

// MyBoundService.java
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

public class MyBoundService extends Service {

    private Handler handler;
    private int count = 0;

    private String logData;
    private static final String TAG = "MyBoundService";

    private final IBinder binder = new LocalBinder();

    public class LocalBinder extends Binder {
        public MyBoundService getService() {
            return MyBoundService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setServiceData("this is a logData");

        handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                count++;
                Log.d(TAG, "Count: " + count);
                handler.postDelayed(this, 1000);
            }
        };
        handler.post(runnable);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public String getServiceData() {
        return logData + count;
    }
    public void setServiceData(String data) {
        this.logData=data;
    }
}
